package com.atguigu.servicebase.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
@Component
public class MyMetaObjecHandler implements MetaObjectHandler {
    /**
     * LocalDateTime localDateTime = LocalDateTime.now();
     *
     * Date date = Date.from( localDateTime.atZone( ZoneId.systemDefault()).toInstant());
     *
     * System.out.println(date);
     *
     *
     *
     * Date todayDate = new Date();
     *
     * LocalDateTime ldt = todayDate.toInstant()
     *         .atZone( ZoneId.systemDefault() )
     *         .toLocalDateTime();
     *
     * System.out.println(ldt);
     *
     * 
     * @param metaObject
     */
    static SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
    @Override
    public void insertFill(MetaObject metaObject) {
        try {
            Date  nowDate = formatter.parse(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            this.setFieldValByName("gmtCreate", nowDate,metaObject);
            this.setFieldValByName("gmtModified",nowDate,metaObject);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            Date  nowDate = formatter.parse(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            this.setFieldValByName("gmtModified",nowDate,metaObject);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
